Kernel.module.define('mediaplayer', {
	
	title: 'mediaplayer',
	file: 'mediaplayer.php',
	
	init: function() {
		var module = this;
		var smv = localStorage.getItem("CONFIG_STARTUP_VOLUME");
		var fsProgress = false;
		var hl = '';
		var lastUsedDir = localStorage.getItem("CONFIG_LAST_MEDIA_DIR");
		var realMediaDir = localStorage.getItem("CONFIG_REAL_MEDIA_DIR");

		if (lastUsedDir == null) {
			lastUsedDir = "";
			localStorage.setItem("CONFIG_LAST_MEDIA_DIR", lastUsedDir);
		} else if (smv == null) {
			smv = 50;
			localStorage.setItem("CONFIG_STARTUP_VOLUME", smv);
		}
		if (realMediaDir == null) {
			realMediaDir = "/media/music/";
			localStorage.setItem("CONFIG_REAL_MEDIA_DIR", realMediaDir);
		}

		$('#mpSliderTime').slider({
			min: 0,
			max: 100,
			value: 0,
			slide: function(e, ui) {
				module.sandbox.audioPositionPercent(parseInt(ui.value));
			}
		});

		$('#mpSliderVolume2').slider({
			min: 0,
			max: 100,
			value: parseInt(smv),
			slide: function(e, ui) {
				module.sandbox.audioVolume(parseInt(ui.value));
				smv = parseInt(ui.value);
			}
		});

		var oDir = "odir="+realMediaDir+"&";
		$.get('modules/'+module.title+'/'+module.file+'/mediaplayer.php?'+oDir+'refresh=1&dir='+lastUsedDir, function(data) {
			$("#mediaBrowserDiv").html(data);
			if (lastUsedDir !== "") $("#mediaParentDiv").fadeIn(1000);
		});

		var ce, scroller;
		ce = document.getElementById('mediaBrowserDiv');
		scroller = new FTScroller(ce, {
			scrollbars: false,
			scrollingX: false
		});
		
		$('#mediaBrowserDiv').on('click', 'li', function() {

			var liContent = $(this).html().split("<p>");
			var art = liContent[0];
			var clickedID = this.id;

			if ($(this).css('background-color')=="rgb(221, 221, 221)") {
				$("#fsplayer").fadeIn(500);
				$("#mediaTitleDiv").fadeOut(500);
				$("#mediaParentDiv").fadeOut(500);
				$("#mediaBrowserDiv").fadeOut(500);
			} else {
				switch (this.id.substr(0, 1)) {
					case '|':
						var oDir = "odir="+localStorage.getItem("CONFIG_REAL_MEDIA_DIR")+"&";
						$.get('modules/'+module.title+'/'+module.file+'/mediaplayer.php?'+oDir+'refresh=1&dir='+clickedID, function(data) {
							$("#mediaBrowserDiv").html(data);
							localStorage.setItem("CONFIG_LAST_MEDIA_DIR", clickedID);
						});
						$("#mediaParentDiv").fadeIn(1000);
						hl = "";

						var ce, scroller;
						ce = document.getElementById('cc');
						scroller = new FTScroller(ce, {
							scrollbars: false,
							scrollingX: false
						});

						break;
					default:
						if (hl !== "") {
							hl.removeClass("highlight");
						}
						hl = $(this);
						$(this).addClass("highlight");
						module.sandbox.audioPlay(this.id, smv, art, $(this).attr('data-id3title'), $(this).attr('data-id3album'), $(this).attr('data-id3artist'), $(this).attr('data-id3genre'));
				}
			}

		});

		$("#mediaParentDiv").on("click", function() {
			if (!fsProgress) {
				var pDir = $("#parentDirDiv").text();
				var oDir = "odir="+localStorage.getItem("CONFIG_REAL_MEDIA_DIR")+"&";
				
				$.get('modules/'+module.title+'/'+module.file+'/mediaplayer.php?'+oDir+'refresh=1&dir=|'+$("#parentDirDiv").text(), function(data) {
					$("#mediaBrowserDiv").html(data);
					localStorage.setItem("CONFIG_LAST_MEDIA_DIR", "|"+pDir);
				});
			} else {
				$("#fsplayer").fadeOut(500);
				$("#mediaBrowserDiv").fadeIn(500);
				//$("#mediaFooterDiv").fadeIn(500);
				fsProgress = false;
			}
		});

		module.sandbox.listen('audio started', function(data) {
			fsProgress = true;
			$("#mpSliderTime").slider("value", 0);
			
			//$("#mediaFooterDiv").hide();
			$("#mediaTitleDiv").fadeOut(500);
			$("#mediaParentDiv").fadeOut(500);
			$("#mediaBrowserDiv").fadeOut(500);
			$("#fsplayer").fadeIn(500);
			$("#fsTitle").html("<span style='color: #005500'>"+module.sandbox.audioPlayingArtist+"</span>");
			if (data.length < 35) {
				$("#fsTitle2").html(data);
			} else {
				$("#fsTitle2").html(data.substr(0, 32)+"...");
			}
			$("#fsCoverArt").html(module.sandbox.audioPlayingArt);
		});

		module.sandbox.listen('audio progressupdate', function(data) {
			$("#mpSliderTime").slider("value", parseInt(data));
			if (fsProgress) {
				$('#duration').html(buzz.toTimer(module.sandbox.audioInstance.getTime())+" ["+buzz.toTimer(module.sandbox.audioInstance.getDuration())+"]");
			}
			if (buzz.toTimer(module.sandbox.audioInstance.getTime()) > buzz.toTimer(module.sandbox.audioInstance.getDuration()-1)) {
				$("#fsNext").trigger("click");
			}
		});

		module.sandbox.listen('audio volumeupdate', function(data) {
			$("#mpSliderVolume").slider("value", parseInt(data));
		});

		module.sandbox.listen('audio end', function(data) {
			if (fsProgress) {
				$("#fsplayer").fadeOut(500);
				$("#mediaTitleDiv").fadeIn(500);
				$("#mediaParentDiv").fadeIn(500);
				$("#mediaBrowserDiv").fadeIn(500);
				//$("#mediaFooterDiv").fadeIn(500);
				fsProgress = false;
			}
			hl.removeClass("highlight");
			hl = '';
			$("#mediaTitleDiv").html("");
		});
		
		$("#fsBack").on("click", function() {
			$("#fsplayer").fadeOut(500);
			$("#mediaTitleDiv").fadeIn(500);
			$("#mediaParentDiv").fadeIn(500);
			$("#mediaBrowserDiv").fadeIn(500);
			//$("#mediaFooterDiv").fadeIn(500);
			fsProgress = false;
		});

		$("#fsPrev").on("click", function() {
			var prevTrack = $("li.highlight").prev();

			if (prevTrack.length > 0) {
				var liContent = prevTrack.html().split("<p>");
				var art = liContent[0];
				module.sandbox.audioPlay(prevTrack.attr('id'), smv, art, prevTrack.attr('data-id3title'), prevTrack.attr('data-id3album'), prevTrack.attr('data-id3artist'), prevTrack.attr('data-id3genre'));
				$("li.highlight").removeClass("highlight");
				prevTrack.addClass("highlight");
				hl = prevTrack;
			}
		});

		$("#fsStop").on("click", function() {
			module.sandbox.audioStop();
		});
		
		$("#fsNext").on("click", function() {
			var nextTrack = $("li.highlight").next();

			if (nextTrack.length > 0) {
				var liContent = nextTrack.html().split("<p>");
				var art = liContent[0];
				module.sandbox.audioPlay(nextTrack.attr('id'), smv, art, nextTrack.attr('data-id3title'), nextTrack.attr('data-id3album'), nextTrack.attr('data-id3artist'), nextTrack.attr('data-id3genre'));
				$("li.highlight").removeClass("highlight");
				nextTrack.addClass("highlight");
				hl = nextTrack;
			}
		});

		$("#fsPlayer, #fscontrols, #fsFooter, #fsBack, fsCoverArt, #mediaTitleDiv document.body #dashAccordion").on("selectstart dragstart", function(e) {
			e.preventDefault();
			e.stopPropagation();
			return false;
		});
	}	
});
 
